package com.eliza.demo.activity

import android.graphics.Color.*
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.eliza.demo.R
import com.eliza.library.tools.InfoTools

class MenuNavActivity : AppCompatActivity() {
    private val context = this
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mnv)
        val navHostFragment = supportFragmentManager.findFragmentById(
            R.id.mnv_host_container
        ) as NavHostFragment
        navController = navHostFragment.navController

    }

    //调用OptionMenu，在这里完成菜单初始化
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menu.add(1, RED, 1, "FG1");
        menu.add(1, GREEN, 2, "FG2");
        menu.add(1, BLUE, 3, "FG3");
        return true;
    }

    //菜单项被选中时触发，这里完成事件处理
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        InfoTools.LogTools("TAG", "::$id")
        when (id) {
            RED -> navController.navigate(R.id.f1Fragment)
            GREEN -> navController.navigate(R.id.f2Fragment)
            BLUE -> navController.navigate(R.id.f3Fragment)
            else -> navController.navigate(R.id.f1Fragment)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onOptionsMenuClosed(menu: Menu?) {
        super.onOptionsMenuClosed(menu)
    }
}